﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using Microsoft.Win32;

namespace JYM.Common
{
    /// <summary>
    /// 电脑硬件信息获取类
    /// </summary>
    public class MachineInfo
    {
        /// <summary>
        /// 获取机器名
        /// </summary>
        /// <returns></returns>
        public string GetHostName()
        {
            return System.Net.Dns.GetHostName();
        }

        #region CUP相关

        /// <summary>
        /// 获取CPU编号
        /// </summary>
        /// <returns></returns>
        public string GetCpuId()
        {

            ManagementClass mc = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = mc.GetInstances();

            String strCpuID = null;
            foreach (ManagementObject mo in moc)
            {
                strCpuID = mo.Properties["ProcessorId"].Value.ToString();
                break;
            }
            return strCpuID;

        }

        /// <summary>
        /// 获取CPU频率
        /// </summary>
        /// <returns>整型cpu频率</returns>       
        public int GetCPUFrequency()
        {
            RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\CentralProcessor\0");

            object obj = rk.GetValue("~MHz");
            int CPUFrequency = (int)obj;
            return CPUFrequency;

        }

        /// <summary>
        /// 获取CPU名称
        /// </summary>
        /// <returns>字符串型cpu名称</returns>
        public string GetCPUName()
        {
            RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\CentralProcessor\0");
            object obj = rk.GetValue("ProcessorNameString");
            string CPUName = (string)obj;
            return CPUName.TrimStart();

        }


        #endregion

        #region 硬盘相关

        /// <summary>
        /// 获取硬盘相应分区的序列号
        /// </summary>
        /// <returns></returns>
        public string GetAllSerialNumber()
        {
            string Dri = "";

            System.Management.ManagementClass mo = new System.Management.ManagementClass("Win32_LogicalDisk");

            System.Management.ManagementObjectCollection mc = mo.GetInstances();

            foreach (System.Management.ManagementObject m in mc)
            {
                if (Convert.ToString(m.Properties["DriveType"].Value) == "3")
                {
                    Dri = Dri + m.Properties["VolumeSerialNumber"].Value.ToString() + "\n";
                }
            }

            Dri = Dri.Substring(0, Dri.Length - 1);

            return Dri;
        }

        /// <summary>
        /// 获取硬盘相应分区的序列号
        /// </summary>
        /// <param name="Drive">盘符（如 C）</param> 
        /// <returns></returns>
        public string GetSpecialVolumeSerialNumber(string Drive)
        {
            string Dri = "";

            System.Management.ManagementClass mo = new System.Management.ManagementClass("Win32_LogicalDisk");

            System.Management.ManagementObjectCollection mc = mo.GetInstances();

            foreach (System.Management.ManagementObject m in mc)
            {
                if (Convert.ToString(m.Properties["DriveType"].Value) == "3")
                {
                    if (m.Properties["Name"].Value.ToString().ToUpper().Trim().Substring(0, 1) == Drive.ToUpper().Trim())
                    {
                        Dri = Dri + m.Properties["VolumeSerialNumber"].Value.ToString();

                        break;
                    }
                }
            }

            return Dri;
        }


        /// <summary>
        /// 磁盘空间
        /// </summary>
        /// <param name="DiskName">硬盘名称:D:或E:</param>
        /// <returns>整型</returns>
        public long GetFreeDiskSpace(string DiskName)
        {
            ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + DiskName + "\"");
            disk.Get();
            string totalByte = disk["FreeSpace"].ToString();
            long freeDiskSpaceMb = Convert.ToInt64(totalByte) / 1024 / 1024;
            return freeDiskSpaceMb;

        }

        #endregion

        /// <summary>
        /// 获取操作系统版本
        /// </summary>
        /// <returns>操作系统版本</returns>
        public string GetOSName()
        {
            string Rev = "";
            System.OperatingSystem osInfo = System.Environment.OSVersion;
            switch (osInfo.Platform)
            {
                //Platform is Windows 95, Windows 98,Windows 98 Second Edition, or Windows Me.
                case System.PlatformID.Win32Windows:
                    switch (osInfo.Version.Major)
                    {
                        case 0:
                            Rev = "Windows 95";
                            break;
                        case 10:
                            if (osInfo.Version.Revision.ToString() == "2222A")
                                Rev = "Windows 98 Second Edition";
                            else
                                Rev = "Windows 98";
                            break;
                        case 90:
                            Rev = "Windows Me";
                            break;
                    }
                    break;

                //Platform is Windows NT 3.51, Windows NT 4.0, Windows 2000,or Windows XP.
                case System.PlatformID.Win32NT:
                    switch (osInfo.Version.Major)
                    {
                        case 3:
                            Rev = "Windows NT 3.51";
                            break;
                        case 4:
                            Rev = "Windows NT 4.0";
                            break;
                        case 5:
                            if (osInfo.Version.Minor == 0)
                                Rev = "Windows 2000";
                            else if (osInfo.Version.Minor == 2)
                                Rev = "Windows 2003";
                            else
                                Rev = "Windows XP";
                            break;
                    }
                    break;
            }
            return Rev;
        }


    }
}
